package com.sskj.gocoin.utils;

import android.text.TextUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;

/**
 * Created by lv on 18-6-5.
 */

public class NumberUtil {


    /**
     * 保留2位小数，如果为0,返回--
     *
     * @param number
     * @return
     */
    public static String keep2(String number) {
        if (number.isEmpty()) {
            return "--";
        }

        if (!isNumeric(number)) {
            return number;
        }
        NumberFormat nf = NumberFormat.getNumberInstance();


        // 保留两位小数
        nf.setMinimumFractionDigits(2);
        nf.setMaximumFractionDigits(2);
        nf.setGroupingUsed(false);

        // 如果不需要四舍五入，可以使用RoundingMode.DOWN
        nf.setRoundingMode(RoundingMode.DOWN);

        return nf.format(Double.valueOf(number));
    }


    /**
     * 保留2位小数，如果为0,返回--
     *
     * @param number
     * @return
     */
    public static String keep2(Double number) {
        NumberFormat nf = NumberFormat.getNumberInstance();
        // 保留两位小数
        nf.setMinimumFractionDigits(2);
        nf.setMaximumFractionDigits(2);
        nf.setGroupingUsed(false);

        // 如果不需要四舍五入，可以使用RoundingMode.DOWN
        nf.setRoundingMode(RoundingMode.DOWN);

        return nf.format(number);
    }

    //保留四位数值，不四舍五入
    public static String keepPoint4(String number) {

        if (number.isEmpty()) {
            return "--";
        }
        if (!isNumeric(number)) {
            return number;
        }

        NumberFormat nf = NumberFormat.getNumberInstance();


        // 保留两位小数
        nf.setMinimumFractionDigits(4);
        nf.setMaximumFractionDigits(4);
        nf.setGroupingUsed(false);


        // 如果不需要四舍五入，可以使用RoundingMode.DOWN
        nf.setRoundingMode(RoundingMode.DOWN);


        return nf.format(Double.valueOf(number));
    }

    /**
     * 保留4位小数，如果为0,返回--
     *
     * @param number
     * @return
     */
    public static String keep4(String number) {
        if (number.equals("---")) {
            return "---";
        }
        if (number.isEmpty()) {
            return "--";
        }
        if (!isNumeric(number)) {
            return number;
        }
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMinimumFractionDigits(4);
        nf.setMaximumFractionDigits(4);
        nf.setGroupingUsed(false);
        nf.setRoundingMode(RoundingMode.UP);
        return nf.format(Double.valueOf(number));
    }

    public static String keep0(String number) {
        if (!isNumeric(number)) {
            return number;
        }

        NumberFormat nf = NumberFormat.getNumberInstance();
        // 保留两位小数
        nf.setMinimumFractionDigits(0);
        nf.setMaximumFractionDigits(0);
        nf.setGroupingUsed(false);

        // 如果不需要四舍五入，可以使用RoundingMode.DOWN
        nf.setRoundingMode(RoundingMode.UP);


        return nf.format(Double.valueOf(number));
    }

    /**
     * 保留最多四位小数，number为空，返回0
     *
     * @param number
     * @return
     */
    public static String keepMax4(String number) {

        if (TextUtils.isEmpty(number)) {
            return 0 + "";
        }
        if (!isNumeric(number)) {
            return number;
        }
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(4);
        nf.setGroupingUsed(false);
        // 如果不需要四舍五入，可以使用RoundingMode.DOWN
        nf.setRoundingMode(RoundingMode.UP);
        return nf.format(Double.valueOf(number));
    }

    public static String keepMax4(Double number) {
        return keepMax4(number + "");
    }

    /**
     * 去除小数点最后的零
     *
     * @param number
     * @return
     */
    public static String keepNoZore(String number) {
        if (!isNumeric(number)) {
            return number;
        }

        if (TextUtils.isEmpty(number)) {
            return 0 + "";
        }
        if (number.indexOf(".") > 0) {
            number = number.replaceAll("0+?$", "");//去掉多余的0
            number = number.replaceAll("[.]$", "");//如最后一位是.则去掉
        }
        return number;
    }

    public static String keepNoZoreMax4(String number) {
        return keepMax4(keepNoZore(number));
    }

    /**
     * 去除小数点最后的零
     *
     * @param number
     * @return
     */
    public static String keepNoZore(double number) {

        return keepNoZore(number + "");
    }

    public static String keepMoney2Dot(String number) {

        NumberFormat numberFormat = NumberFormat.getNumberInstance();

        numberFormat.setMaximumFractionDigits(2);
        numberFormat.setMinimumFractionDigits(2);


        String numberString = numberFormat.format(Double.valueOf(number));
//        NumberFormat nf = new DecimalFormat("#,###.##");
        return numberString;
    }

    public static String keepMoneyWithDot(String number, int dot) {

        NumberFormat numberFormat = NumberFormat.getNumberInstance();

        numberFormat.setMaximumFractionDigits(dot);
        numberFormat.setMinimumFractionDigits(dot);


        String numberString = numberFormat.format(Double.valueOf(number));
        return numberString;
    }

    public static String keepMoneyNoZoreWithMaxDot(String number, int dot) {

        return keepNoZore(keepMoneyWithDot(number, dot));
    }

    /**
     * 去除小数点最后的零
     *
     * @param number 原数据
     * @return 处理后数据
     */
    public static String keepNoZero(String number) {

        if (TextUtils.isEmpty(number)) {
            return 0 + "";
        }
        if (!isNumeric(number)) {
            return number;
        }

        if (number.indexOf(".") > 0) {
            number = number.replaceAll("0+?$", "");//去掉多余的0
            number = number.replaceAll("[.]$", "");//如最后一位是.则去掉
        }
        return number;
    }

    public static String keepNoIn(String number, int keepNum, boolean isMax) {
        if (TextUtils.isEmpty(number)) {
            return 0 + "";
        }
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(keepNum);
        if (!isMax) {
            nf.setMinimumFractionDigits(keepNum);
        }
        nf.setGroupingUsed(false);
        // 如果不需要四舍五入，可以使用RoundingMode.DOWN
        nf.setRoundingMode(RoundingMode.DOWN);
        return nf.format(new BigDecimal(number).doubleValue());
    }


    public static boolean isNumeric(String str) {
        String reg = "^[0-9]+(.[0-9]+)?$";
        return str.matches(reg);
    }
}
